function tea_encrypt(src, key) {
  var k = new Uint32Array(4);
  var v = new Uint32Array(2);
  var dst = [];
  // 将密钥转换为Uint32Array
  for (var i = 0; i < 4; i++) {
      k[i] = (key.charCodeAt(i * 4) << 24) |
              (key.charCodeAt(i * 4 + 1) << 16) |
              (key.charCodeAt(i * 4 + 2) << 8) |
              key.charCodeAt(i * 4 + 3);
  }
  // 每8个字符进行加密
  var count = Math.floor(src.length / 8);
  for (var i = 0; i < count; i++) {
      v = new Uint32Array(2);
      
      for (var j = 0; j < 8; j++) {
          v[j >> 2] |=(src.charCodeAt(i * 8 + j) << ((j % 4) * 8));
      }
      encryptNew(v, k);
      for (var j = 0; j < 8; j++) {
          dst[i * 8 + j] = (v[j >> 2] >>> ((j % 4) * 8)) & 0xFF;
      }
  }
  // 返回加密后的结果
  return dst;
}
//加密函数
function encryptNew(v, k) {
  let x = v[0], y = v[1], sum = 0;
  // console.log("before:","x=" + x ,"y=" + y);
  const delta = 0x9e3779b9;
  let k0 = k[0], k1 = k[1], k2 = k[2], k3 = k[3];
  for (let i = 0; i < 32; i++) {
      sum += delta;
      x += ((y << 4) + k0) ^ (y + sum) ^ ((y >>> 5) + k1);
      y += ((x << 4) + k2) ^ (x + sum) ^ ((x >>> 5) + k3);
  }
  // console.log("after4:","x=" + x ,"y=" + y);
  v[0] = x;
  // console.log("函数内v[0]=" + v[0]);
  v[1] = y;
}

module.exports = {
  tea_encrypt,
  encryptNew
}

